package com.yoda.mall.admin.controller;

import com.yoda.mall.admin.service.ProductCategoryService;
import com.yoda.mall.model.dto.ProductCategoryDto;
import com.yoda.mall.model.entity.pms.ProductCategory;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.ProductCategoryVo;
import com.yoda.mall.model.vo.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shawn
 * @date 2022年 12月 17日 23:44
 */
@RestController
@RequestMapping("/productCategory")
public class ProductCategoryController {

    @Resource
    private ProductCategoryService productCategoryService;

    @GetMapping("/list/withChildren")
    public R GetCategoryWithChildren(){
       List<ProductCategoryVo> list = productCategoryService.GetCategoryWithChildren();
       return R.success(list);
    }
    @GetMapping("/list/{parentId}")
    public R getCategoryByPage(@PathVariable Long parentId,@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize){
        PageResult<ProductCategoryVo> pageResult = productCategoryService.getCategoryByPage(parentId,pageNum,pageSize);
       return R.success(pageResult);
    }

    /**
     * 通过id获取类别
     *
     * @param id id
     * @return {@link R}
     */
    @GetMapping("/{id}")
    public R getCategoryById(@PathVariable Long id){
        ProductCategory productCategory = productCategoryService.getCategoryById(id);
        return R.success(productCategory);
    }

    /**
     * 创建类别
     *
     * @param dto dto
     * @return {@link R}
     */
    @PostMapping("/create")
    public R createCategory(@RequestBody ProductCategoryDto dto){

        Integer count = productCategoryService.createCategory(dto);
        return R.success(count);
    }

    /**
     * 删除类别
     *
     * @param id id
     * @return {@link R}
     */
    @PostMapping("//delete/{id}")
    public R deleteCategory(@PathVariable Long id){

        Integer count = productCategoryService.deleteCategoryById(id);
        return R.success(count);
    }

    /**
     * 更新类别
     *
     * @param id  id
     * @param dto dto
     * @return {@link R}
     */
    @PostMapping("/update/{id}")
    public R updateCategory(@PathVariable Long id,@RequestBody ProductCategoryDto dto){
        Integer count = productCategoryService.updateCategoryById(id,dto);
        return R.success(count);
    }

    /**
     * 改变显示状态
     *
     * @param showStatus 显示状态
     * @param ids        id
     * @return {@link R}
     */
    @PostMapping("/update/showStatus")
    public R changeShowStatus(@RequestParam Integer showStatus,@RequestParam List<Long> ids){
        Integer count = productCategoryService.changeShowStatus(showStatus,ids);
        return R.success(count);
    }

    @PostMapping("/update/navStatus")
    public R changeNavStatus(@RequestParam Integer navStatus,@RequestParam List<Long> ids){
        Integer count = productCategoryService.changeNavStatus(navStatus,ids);
        return R.success(count);
    }

}
